package com.beiye.drivertransport.thematic.learning.examiantion;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.android.frame.adapter.CommonAdapter;
import com.android.frame.adapter.ViewHolder;
import com.beiye.drivertransport.R;
import com.beiye.drivertransport.activity.TwoBaseAty;
import com.beiye.drivertransport.bean.DaikaoAndHavetestCourseBean;
import com.beiye.drivertransport.bean.ExaminationPhotosBean;
import com.beiye.drivertransport.config.AppInterfaceConfig;
import com.beiye.drivertransport.http.Login;
import com.beiye.drivertransport.utils.Utils;
import com.beiye.drivertransport.view.CustomView;
import com.githang.statusbar.StatusBarCompat;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.squareup.picasso.Picasso;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

//专项已考考试试卷
public class HaveExaminationthematiclearningActivity extends TwoBaseAty {

    ImageView img_examback;

    ListView listview_havetesttest;

    TextView tv_creatdate;//发布日期
//    @Bind(R.id.tv_havetest1)
//    TextView tv_type;//试卷种类

    TextView tv_name;//试卷名称

    TextView tv_total;//总分

    TextView tv_jigescore;//及格

    TextView tv_score;//得分

    TextView tv_havetest8;//已拍照片

    TextView tv_havetest9;//查看签名

    ImageView tv_jige;//是否及格

    private SubHaveTestAdapter subHaveTestAdapter;
    private int sn;
    private int qpsn;
    private List<DaikaoAndHavetestCourseBean.RowsBean> rowslist;
    private TextView mTextView;
    private CustomView customView;
    private String signPicUrl;
    private Long testEndDate;

    @Override
    public int getLayoutId() {
        return R.layout.activity_have_examinationthematiclearning;
    }

    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.img_examback) {
            finish();
        } else if (id == R.id.tv_havetest8) {//已拍照片
            if (Utils.isFastDoubleClick()) {
                return;
            }
//               Bundle bundle = new Bundle();
//                bundle.putInt("uqpSn",sn);
//                startActivity(ExaminationPhotosActivity.class, bundle);
            //查询考试照片
            OkGo.get(AppInterfaceConfig.BASE_URL + "course/userQpPhoto/" + sn)
                    .tag(this)
                    .execute(new StringCallback() {
                        @Override
                        public void onSuccess(String result, Call call, Response response) {

                            ExaminationPhotosBean examinationPhotosBean = JSON.parseObject(result, ExaminationPhotosBean.class);
                            List<ExaminationPhotosBean.RowsBean> rows = examinationPhotosBean.getRows();
                            if (rows.size() == 0) {
                                showToast("暂无考试照片");
                                return;
                            }
                            String url = rows.get(0).getUrl();
                            showpohotowindow(url);

                        }

                        @Override
                        public void onError(Call call, Response response, Exception e) {
                            super.onError(call, response, e);

                        }
                    });
        } else if (id == R.id.tv_havetest9) {//查看签名
            if (Utils.isFastDoubleClick()) {
                return;
            }
//                Bundle bundle1 = new Bundle();
//                bundle1.putString("signPicUrl",signPicUrl);
//               startActivity(SignCourseActivity.class, bundle1);
            showsignwindow(signPicUrl);
        }

    }
//签名弹窗
    private void showsignwindow(String signPicUrl) {
        AlertDialog.Builder builder = new AlertDialog.Builder(HaveExaminationthematiclearningActivity.this);
        View view1 = View.inflate(HaveExaminationthematiclearningActivity.this, R.layout.diolog_learn_layout, null);
        TextView tv_message = (TextView) view1.findViewById(R.id.message);//输入内容
        ImageView img_erweima1 = (ImageView) view1.findViewById(R.id.img_erweima1);//输入内容
        tv_message.setText("考试签名：");
        TextView btn_sure = (TextView) view1.findViewById(R.id.positiveButton);
        TextView btn_cancel = (TextView) view1.findViewById(R.id.negativeButton);
        btn_sure.setText("我知道");
        btn_cancel.setVisibility(View.GONE);
        img_erweima1.setVisibility(View.VISIBLE);
        Picasso.with(HaveExaminationthematiclearningActivity.this).load(Uri.parse(signPicUrl)).placeholder(R.mipmap.no_data1).into(img_erweima1);
        final AlertDialog dialog = builder.create();
        dialog.show();
        dialog.getWindow().setContentView(view1);
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        dialog.setCancelable(true);
        btn_sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();

            }
        });
    }
    //考试照片弹窗
    private void showpohotowindow(String url) {
        AlertDialog.Builder builder = new AlertDialog.Builder(HaveExaminationthematiclearningActivity.this);
        View view1 = View.inflate(HaveExaminationthematiclearningActivity.this, R.layout.diolog_learn_layout, null);
        TextView tv_message = (TextView) view1.findViewById(R.id.message);//输入内容
        ImageView img_erweima = (ImageView) view1.findViewById(R.id.img_erweima);//输入内容
        tv_message.setText("考试照片：");
        TextView btn_sure = (TextView) view1.findViewById(R.id.positiveButton);
        TextView btn_cancel = (TextView) view1.findViewById(R.id.negativeButton);
        btn_sure.setText("我知道");
        btn_cancel.setVisibility(View.GONE);
        img_erweima.setVisibility(View.VISIBLE);
        Picasso.with(HaveExaminationthematiclearningActivity.this).load(Uri.parse(url)).placeholder(R.mipmap.no_data).into(img_erweima);
        final AlertDialog dialog = builder.create();
        dialog.show();
        dialog.getWindow().setContentView(view1);
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        dialog.setCancelable(true);
        btn_sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();

            }
        });


    }

    @Override
    public void initData() {
        img_examback = (ImageView) findViewById(R.id.img_examback);
        listview_havetesttest = (ListView) findViewById(R.id.listview_havetesttest);
        tv_creatdate = (TextView)findViewById(R.id.tv_havetest);
        tv_name = (TextView)findViewById(R.id.tv_havetest2);
        tv_total = (TextView)findViewById(R.id.tv_havetest3);
        tv_jigescore = (TextView)findViewById(R.id.tv_havetest7);
        tv_score = (TextView)findViewById(R.id.tv_havetest5);
        tv_havetest8 = (TextView)findViewById(R.id.tv_havetest8);
        tv_havetest9 = (TextView)findViewById(R.id.tv_havetest9);
        tv_jige = (ImageView)findViewById(R.id.tv_havetest6);

        StatusBarCompat.setStatusBarColor(this, Color.parseColor("#1F80C4"));
        Bundle bundle = getIntent().getExtras();
        //通过name关键字取出用户名
        sn = bundle.getInt("havesn");
        Long  havetestcreation = bundle.getLong("havecreation");
        testEndDate = bundle.getLong("testEndDate");
        signPicUrl = bundle.getString("signPicUrl");
        qpsn = bundle.getInt("haveqpsn");
        Date date=new Date(havetestcreation);
        String time="";
        try {
            //yyyy表示年MM表示月dd表示日
            //yyyy-MM-dd是日期的格式，比如2015-12-12如果你要得到2015年12月12日就换成yyyy年MM月dd日
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日");
            //进行格式化
            time=sdf.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        tv_creatdate.setText("发布日期:"+time);
        String haveqptName = bundle.getString("haveqptName");
        String haveqpName = bundle.getString("haveqpName");
        int havetotalScore = bundle.getInt("havetotalScore");
        int havepassScore = bundle.getInt("havepassScore");
        int havescore = bundle.getInt("havescore");
        int havepassMark = bundle.getInt("havepassMark");
     //   tv_type.setText("试卷种类："+haveqptName);
        tv_name.setText("试卷名称："+haveqpName);
        tv_total.setText("总分数："+havetotalScore);
        tv_jigescore.setText("及格分数："+havepassScore);
        tv_score.setText(havescore+"");
        if(havepassMark==1)
        {
            tv_jige.setImageResource(R.mipmap.jige);
//                tv_jige.setText("及格");
//                tv_jige.setTextColor(getBaseContext().getResources().getColor(R.color.text_frame_color));
            //   tv_jige.setBackgroundResource(R.drawable.shape_text);
        }else{
            tv_jige.setImageResource(R.mipmap.nojige);
//                tv_jige.setText("不及格");
//                tv_jige.setTextColor(getBaseContext().getResources().getColor(R.color.text_red));
            //    tv_jige.setBackgroundResource(R.drawable.shape_text_red);
        }

        customView = (CustomView) findViewById(R.id.customView);
        mTextView = (TextView) findViewById(R.id.letter_Tv);


        customView.setOnTouchMyLister(new CustomView.OnTouchLister() {
            @Override
            public void onTouch(int s) {
                mTextView.removeCallbacks(action);
                mTextView.setText(s+"");
                mTextView.setVisibility(View.VISIBLE);
                if (1==s)
                {
                    listview_havetesttest.setSelection(0);
                }
                for (int i = 0; i < rowslist.size(); i++)
                {
                    int seqNo = rowslist.get(i).getSeqNo();
                    if (seqNo==s)
                    {
                        listview_havetesttest.setAdapter(subHaveTestAdapter);
                        listview_havetesttest.requestFocusFromTouch();
                        listview_havetesttest.setSelection(i);

                        break;
                    }
                }
            }

            @Override
            public void up() {
                mTextView.postDelayed(action, 1500);
            }
        });

//        numview.setOnLettersListViewListener(new NumView.OnLettersListViewListener() {
//            @Override
//            public void onLettersListener(String s) {
//              int  num = Integer.parseInt(s);
//                if (1==num)
//                {
//                    listview_havetesttest.setAdapter(subHaveTestAdapter);
//                    listview_havetesttest.setSelection(0);
//                }
//                for (int i = 0; i < rowslist.size(); i++)
//                {
//                    int seqNo = rowslist.get(i).getSeqNo();
//                    if (seqNo==num)
//                    {
//                        listview_havetesttest.setAdapter(subHaveTestAdapter);
//                        listview_havetesttest.setSelection(i);
//                        break;
//                    }
//                }
//            }
//        });
    }

    Runnable action = new Runnable()
    {
        public void run()
        {
            mTextView.setVisibility(View.GONE);
        }
    };

    @Override
    public void requestData() {

         showLoadingDialog("");
        //已考试卷详情
        new Login().getHavecourse(sn,1,1000,this,1);

    }
    @Override
    public void onSuccess(String result, Call call, Response response, int what) {
        super.onSuccess(result, call, response, what);
        if(what==1)
        {
            dismissLoadingDialog();
            DaikaoAndHavetestCourseBean courseBean = JSON.parseObject(result, DaikaoAndHavetestCourseBean.class);
            rowslist = courseBean.getRows();
            for(int i=0;i<rowslist.size();i++){
                DaikaoAndHavetestCourseBean.RowsBean temp = rowslist.get(i);
                temp.setSeqNo(i+1);
            }
            customView.setCount(rowslist.size());
            subHaveTestAdapter = new SubHaveTestAdapter(HaveExaminationthematiclearningActivity.this,rowslist,R.layout.subhavetest_item);
            listview_havetesttest.setAdapter(subHaveTestAdapter);
        }
    }

    public class SubHaveTestAdapter  extends CommonAdapter<DaikaoAndHavetestCourseBean.RowsBean> {
        private Context context;
        private List<DaikaoAndHavetestCourseBean.RowsBean> mList;
        public SubHaveTestAdapter(Context context, List<DaikaoAndHavetestCourseBean.RowsBean> mList, int itemLayoutId) {
            super(context, mList, itemLayoutId);
            this.context = context;
            this.mList=mList;

        }



        @Override
        public void convert(ViewHolder holder, DaikaoAndHavetestCourseBean.RowsBean item, int positon) {
            TextView tv_des = holder.getView(R.id.tv_havetest_test);
            TextView tv_desQ = holder.getView(R.id.tv_havetest_test1);//案例题
            TextView tv_questions = holder.getView(R.id.tv_questions);//案例题
            TextView tv_type = holder.getView(R.id.tv_type);//类型题
            TextView tv_havetestA = holder.getView(R.id.radioButton_havetest);
            TextView tv_havetestB = holder.getView(R.id.radioButton_havetest1);
            TextView tv_havetestC = holder.getView(R.id.radioButton_havetest2);
            TextView tv_havetestD = holder.getView(R.id.radioButton_havetest3);
            TextView tv_havetestE = holder.getView(R.id.radioButton_havetest4);
            TextView tv_havetestF = holder.getView(R.id.radioButton_havetest5);
            ImageView img_havetest = holder.getView(R.id.img_daikao_test_item);
            if (mList.get(positon).getUrl() == null) {
                img_havetest.setVisibility(View.GONE);

            } else {
                img_havetest.setVisibility(View.VISIBLE);
                if(mList.get(positon).getUrl().contains("aliyuncs.com"))
                {
                    Picasso.with(context).load(Uri.parse(mList.get(positon).getUrl())).placeholder(R.mipmap.no_data2).into(img_havetest);
                }else{

                    Picasso.with(context).load(Uri.parse(AppInterfaceConfig.getRequestUrl(mList.get(positon).getUrl()))).placeholder(R.mipmap.no_data2).into(img_havetest);
                }
            }
            TextView tv_right = holder.getView(R.id.tv_havetest7);//正确选项
            TextView tv_choice = holder.getView(R.id.tv_havetest8);//用户选项
            TextView tv_score= holder.getView(R.id.tv_havetest9);//分数
            ImageView tv_rightMark= holder.getView(R.id.tv_havetest10);//正确标记

            int relSn = mList.get(positon).getRelSn();
            String questionDesc2 = mList.get(positon).getQuestionDesc2();
            int qsn = mList.get(positon).getqSn();
            if(relSn==0)
            {
                tv_desQ.setVisibility(View.GONE);
                tv_questions.setVisibility(View.GONE);
            }else{
                if(qsn==relSn)
                {
                    tv_desQ.setVisibility(View.VISIBLE);
                    if(questionDesc2==null)
                    {
                        tv_desQ.setText("");
                        tv_questions.setVisibility(View.VISIBLE);
                    }else{
                        tv_questions.setVisibility(View.VISIBLE);
                        tv_desQ.setText("案例题描述："+questionDesc2);
                    }
                }else{
                    tv_questions.setVisibility(View.VISIBLE);
                    tv_desQ.setVisibility(View.GONE);
                }
            }
            tv_type.setVisibility(View.VISIBLE);
            tv_type.setText(DaikaoAndHavetestCourseBean.getQuestionTypeStr(mList.get(positon).getQtype()));
            tv_des.setText(mList.get(positon).getSeqNo() + ". " + mList.get(positon).getQuestionDesc());
            if (mList.get(positon).getOptionA() == null||mList.get(positon).getOptionA().equals("")) {
                tv_havetestA.setVisibility(View.GONE);

            } else {
                tv_havetestA.setVisibility(View.VISIBLE);
                tv_havetestA.setText("A. "+mList.get(positon).getOptionA());
            }
            if (mList.get(positon).getOptionB() == null||mList.get(positon).getOptionB().equals("")) {
                tv_havetestB.setVisibility(View.GONE);

            } else {
                tv_havetestB.setVisibility(View.VISIBLE);
                tv_havetestB.setText("B. "+mList.get(positon).getOptionB());
            }

            if (mList.get(positon).getOptionC() == null||mList.get(positon).getOptionC().equals("")) {
                tv_havetestC.setVisibility(View.GONE);

            } else {
                tv_havetestC.setVisibility(View.VISIBLE);
                tv_havetestC.setText("C. "+mList.get(positon).getOptionC());
            }


            if (mList.get(positon).getOptionD() == null||mList.get(positon).getOptionD().equals("")) {
                tv_havetestD.setVisibility(View.GONE);

            } else {
                tv_havetestD.setVisibility(View.VISIBLE);
                tv_havetestD.setText("D. "+mList.get(positon).getOptionD());
            }
            if (mList.get(positon).getOptionE() == null||mList.get(positon).getOptionE().equals("")) {
                tv_havetestE.setVisibility(View.GONE);

            } else {
                tv_havetestE.setVisibility(View.VISIBLE);
                tv_havetestE.setText("E. "+mList.get(positon).getOptionE());
            }
            if (mList.get(positon).getOptionF() == null||mList.get(positon).getOptionF().equals("")) {
                tv_havetestF.setVisibility(View.GONE);

            } else {
                tv_havetestF.setVisibility(View.VISIBLE);
                tv_havetestF.setText("F. "+mList.get(positon).getOptionF());
            }


            if(mList.get(positon).getRightOption()!=null)
            {
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//年-月-日 时-分
                Date date=new Date(testEndDate);
                String  testEndStr=dateFormat.format(date);//考試結束日期
                Date curDate =  new Date(System.currentTimeMillis());
                String   curstr   =dateFormat.format(curDate);//当前日期
                try {

                    Date testEndDate = dateFormat.parse(testEndStr);//考試結束日期
                    Date curstrDate = dateFormat.parse(curstr);//当前日期
                    if(curstrDate.getTime()>testEndDate.getTime())
                    {
                        tv_right.setText("正确选项："+mList.get(positon).getRightOption());
                    }else{
                        tv_right.setVisibility(View.GONE);
                    }
                } catch (ParseException e) {
                    e.printStackTrace();
                }



            }
            if(mList.get(positon).getUserOption()!=null)
            {

                tv_choice.setText("您的选择："+mList.get(positon).getUserOption());
            }else{
                tv_choice.setText("您的选择：");
            }
            tv_score.setText("分数："+mList.get(positon).getScore()+"分");
            if(mList.get(positon).getRightMark()==1)
            {
                tv_rightMark.setImageResource(R.mipmap.sure);
//            tv_rightMark.setText("正确");
//            tv_rightMark.setTextColor(context.getResources().getColor(R.color.text_frame_color));
//            tv_rightMark.setBackgroundResource(R.drawable.shape_text);
            }else if(mList.get(positon).getRightMark()==0){
                tv_rightMark.setImageResource(R.mipmap.wrong);
//            tv_rightMark.setText("错误");
//            tv_rightMark.setTextColor(context.getResources().getColor(R.color.text_red));
//            tv_rightMark.setBackgroundResource(R.drawable.shape_text_red);
            }

        }
    }

}